using MyFramework.Event;
using MyFramework.Mono;
using MyFramework.Singleton;
using UnityEngine;

namespace MyFramework.Input
{
    /// <summary>
    /// 1.Input类
    /// 2.事件中心模块和公共Mono模块
    /// </summary>
    public class InputManager : Singleton<InputManager>
    {
        private bool _isEnable = true;

        public InputManager()
        {
            //添加公共Mono模块的Update监听
            MonoManager.GetInstance().AddUpdateListener(Update);
        }

        public void SetEnable(bool value)
        {
            _isEnable = value;
        }

        private void Update()
        {
            if (!_isEnable)
                return;

            CheckKeyCode(KeyCode.W);
            CheckKeyCode(KeyCode.A);
        }

        private void CheckKeyCode(KeyCode key)
        {
            if (UnityEngine.Input.GetKeyDown(key))
                //事件中心模块分发按下事件
                EventCenterManager.GetInstance().EventTrigger("某键按下", key);

            if (UnityEngine.Input.GetKeyUp(key))
                //事件中心模块分发抬起事件
                EventCenterManager.GetInstance().EventTrigger("某键抬起", key);
        }
        
        private void OnApplicationQuit()
        {
            //退出游戏时，移除公共Mono模块的Update监听
            MonoManager.GetInstance().RemoveUpdateListener(Update);
        }
    }
}
